home *** CD-ROM | disk | FTP | other *** search
/ Greenhouse Effect Detection Expriment / NASA Greenhouse Effect Detection Expriment 1992 - Disc 2.iso / software / dos / cdf22pc / src / include / dynaptr.h < prev    next >
Encoding:
C/C++ Source or Header  |  1992-02-27  |  2.2 KB  |  64 lines

  1. /******************************************************************************
  2. *
  3. *  NSSDC/CDF            Header file for dynamic arrays of pointers.
  4. *
  5. *  Version 2.0, 27-Feb-92, ST Systems (STX)
  6. *
  7. *  Modification history:
  8. *
  9. *   V1.0  24-Jan-91, D Grogan/H Leckner       Original version (for CDF V2.0).
  10. *   V1.1  27-Feb-92, J Love           Modified for IBM-PC port.  CDF V2.2.
  11. *             H Leckner
  12. *
  13. ******************************************************************************/
  14.  
  15. /*
  16.     Use LARGE model on PC.
  17.  
  18.     See the 3 key macros, used in this order:
  19.     1)  PTR_LIST_MALLOC initializes a list of n pointers to any type.
  20.         For example,
  21.             char **myPagetext;
  22.             long nLines=60;
  23.             myPageText = PTR_LIST_MALLOC( nLines, char); 
  24.  
  25.     2)  FIXARRAY_MALLOC establishes a fixed number of items of any type
  26.         for the initialized pointer-list to point to.
  27.             long lineSize=81;
  28.             FIXARRAY_MALLOC( myPageText, nLines, char, lineSize);
  29.         Test the first item for success: if (myPageText[0] != NULL) OK;
  30.         If you need a varying length for each item pointed-to,
  31.         then just malloc it yourself, and don't use FIXARRAY_MALLOC:
  32.             for(k=0; k<nLines; k++) myPageText[k] = malloc(need[k]);
  33.  
  34.     3)  PTR_LIST_FREE undoes what PTR_LIST_MALLOC does, leaving no dangling 
  35.         pointers.
  36.             PTR_LIST_FREE( myTextPage, nLines);
  37. */
  38.  
  39.         /* This macro will produce an array[np] of pointers to type */
  40.         /* You must test the return pointer yourself for NULL value */
  41. #define PTR_LIST_MALLOC(np,type)  (type **) malloc( np * sizeof( type *) )
  42.  
  43.         /* This macro will allocate nElem of type to each p[0..np-1] */ 
  44.         /* It tests each malloc for NULL */
  45. #define FIXARRAY_MALLOC(p,np,type,nElem) {long j;/*, err; err=0;*/    \
  46.         for (j=0; j<np; j++) {                    \
  47.             p[j] = (type *) malloc( nElem * sizeof(type) ); \
  48.             if (p[j] == NULL) { long k;            \
  49.                 for (k=0; k<j; k++) {            \
  50.                     free( p[k]);/* free & return */    \
  51.                     p[k] = NULL;}            \
  52.                 break;                    \
  53.             }                        \
  54.         } }  /* end FIXARRAY_MALLOC */
  55.  
  56.     /* This macro frees the PTR list, p[0..np-1], leaving no danglers.  */
  57.     /* And then it frees p */
  58. #define PTR_LIST_FREE(p,np)      {long j;                \
  59.                 for (j=0; j<np; j++) {            \
  60.                     free( p[j]); p[j] = NULL; }    \
  61.                 free( p); p = NULL;}    
  62.                         /* end PTR_LIST_FREE */
  63.  
  64.